/*==============================================================================
案例 6H：房地产价格预测分析（H2O 高级版）
================================================================================

业务场景：
房地产投资公司需要准确预测房产价格，以便做出明智的投资决策。
本案例使用 Stata 19 的 H2O 机器学习功能，对比传统回归和高级机器学习方法。

学习目标：
1. 掌握 H2O GBM 和 Random Forest 在房价预测中的应用
2. 学习特征工程和超参数调优
3. 理解模型集成和 AutoML
4. 对比不同方法的预测性能

技术要求：
- Stata 19+
- H2O 集成功能

数据来源：auto.dta（转换为房产数据）

作者：Stata ML Course
日期：2025-11-03
==============================================================================*/

clear all
set more off
capture log close

* 设置工作目录
cd "`c(pwd)'"

* 创建输出目录
capture mkdir "output/cases_h2o"
capture mkdir "output/cases_h2o/figures"
capture mkdir "data/cases_h2o"

* 开始日志记录
log using "output/cases_h2o/case06h_house_price_h2o.log", replace text

display "=========================================="
display "案例 6H：房地产价格预测分析（H2O 高级版）"
display "=========================================="
display ""

/*------------------------------------------------------------------------------
第一部分：数据准备
------------------------------------------------------------------------------*/

display "第一部分：数据准备"
display "------------------"

* 加载数据并转换为房产数据
sysuse auto, clear

* 重命名变量以匹配房产场景
rename price house_price
rename mpg energy_efficiency
rename weight house_size
rename length lot_size
rename turn accessibility
rename displacement total_rooms
rename gear_ratio floor_ratio
rename headroom ceiling_height

* 特征工程
gen size_sq = house_size^2
gen lot_sq = lot_size^2
gen rooms_sq = total_rooms^2
gen size_lot = house_size * lot_size
gen size_rooms = house_size * total_rooms
gen efficiency_size = energy_efficiency * house_size

* 对数转换
gen log_price = log(house_price)
gen log_size = log(house_size)
gen log_lot = log(lot_size)

* 虚拟变量
tab foreign, gen(location_)
tab rep78, gen(condition_)

* 标准化
egen size_std = std(house_size)
egen lot_std = std(lot_size)
egen rooms_std = std(total_rooms)

display ""
display "数据准备完成"
display "观测数：" _N
display ""

/*------------------------------------------------------------------------------
第二部分：Stata 18 基础版回归（对比基准）
------------------------------------------------------------------------------*/

display "第二部分：Stata 18 基础版回归（对比基准）"
display "----------------------------------------"

* 1. 多项式回归
display ""
display "1. 多项式回归"
regress house_price house_size size_sq lot_size lot_sq total_rooms rooms_sq ///
    energy_efficiency foreign

* 保存预测值
predict price_poly, xb
gen error_poly = house_price - price_poly
gen error_sq_poly = error_poly^2

* 计算性能
quietly correlate house_price price_poly
scalar r2_poly = r(rho)^2
display "多项式回归 R² = " r2_poly

quietly summarize error_sq_poly
scalar rmse_poly = sqrt(r(mean))
display "多项式回归 RMSE = " rmse_poly

* 2. Lasso 回归
display ""
display "2. Lasso 回归"
lasso linear house_price house_size size_sq lot_size lot_sq total_rooms rooms_sq ///
    energy_efficiency size_lot size_rooms efficiency_size ///
    foreign location_* condition_*, ///
    selection(cv, folds(5)) rseed(123)

* 保存预测值
predict price_lasso, xb
gen error_lasso = house_price - price_lasso
gen error_sq_lasso = error_lasso^2

* 计算性能
quietly correlate house_price price_lasso
scalar r2_lasso = r(rho)^2
display "Lasso R² = " r2_lasso

quietly summarize error_sq_lasso
scalar rmse_lasso = sqrt(r(mean))
display "Lasso RMSE = " rmse_lasso

display ""
display "基础版回归完成"
display ""

/*------------------------------------------------------------------------------
第三部分：H2O 高级版回归
------------------------------------------------------------------------------*/

display "第三部分：H2O 高级版回归"
display "------------------------"

* 初始化 H2O 集群
display ""
display "初始化 H2O 集群..."
h2o init

* 导入数据到 H2O frame
display ""
display "导入数据到 H2O..."
_h2oframe put, into(house_data) current

* 划分训练集和测试集（80/20）
display ""
display "划分训练集和测试集..."
_h2oframe split house_data, into(train test) split(0.8 0.2) rseed(123)

* 切换到训练集
_h2oframe change train

/*------------------------------------------------------------------------------
3.1 H2O GBM 回归（基础版）
------------------------------------------------------------------------------*/

display ""
display "3.1 训练 H2O GBM 回归（基础版）..."

h2oml gbregress house_price house_size size_sq lot_size lot_sq total_rooms ///
    rooms_sq energy_efficiency size_lot size_rooms efficiency_size ///
    foreign location_* condition_*, ///
    h2orseed(123) cv(5)

* 保存模型
h2omlest store gbm_basic

* 查看性能
display ""
display "GBM 基础版性能："
h2omlestat metrics

* 变量重要性
display ""
display "变量重要性分析："
h2omlgraph varimp, ///
    title("GBM 变量重要性 - 房价预测") ///
    saving("output/cases_h2o/figures/case06h_gbm_varimp.gph", replace)

/*------------------------------------------------------------------------------
3.2 H2O GBM 回归（超参数调优）
------------------------------------------------------------------------------*/

display ""
display "3.2 训练 H2O GBM 回归（超参数调优）..."

h2oml gbregress house_price house_size size_sq lot_size lot_sq total_rooms ///
    rooms_sq energy_efficiency size_lot size_rooms efficiency_size ///
    foreign location_* condition_*, ///
    h2orseed(123) cv(5) ///
    ntrees(100(100)400) ///
    lrate(0.01(0.02)0.1) ///
    maxdepth(4(2)10) ///
    minrows(3(2)9) ///
    tune(metric(rmse) grid(random) maxmodels(30))

* 保存模型
h2omlest store gbm_tuned

* 查看网格搜索结果
display ""
display "GBM 超参数调优结果："
h2omlestat gridsummary

* 查看最佳模型性能
display ""
display "GBM 最佳模型性能："
h2omlestat metrics

/*------------------------------------------------------------------------------
3.3 H2O Random Forest 回归
------------------------------------------------------------------------------*/

display ""
display "3.3 训练 H2O Random Forest 回归..."

h2oml rfregress house_price house_size size_sq lot_size lot_sq total_rooms ///
    rooms_sq energy_efficiency size_lot size_rooms efficiency_size ///
    foreign location_* condition_*, ///
    h2orseed(123) cv(5) ///
    ntrees(100(100)400) ///
    maxdepth(10(5)25) ///
    mtries(3(2)9) ///
    tune(metric(rmse) grid(random) maxmodels(25))

* 保存模型
h2omlest store rf_tuned

* 查看性能
display ""
display "Random Forest 性能："
h2omlestat metrics

/*------------------------------------------------------------------------------
3.4 H2O Deep Learning 回归
------------------------------------------------------------------------------*/

display ""
display "3.4 训练 H2O Deep Learning 回归..."

h2oml dlregress house_price house_size size_sq lot_size lot_sq total_rooms ///
    rooms_sq energy_efficiency size_lot size_rooms efficiency_size ///
    foreign location_* condition_*, ///
    h2orseed(123) cv(5) ///
    hidden(50 50) ///
    epochs(100) ///
    activation(rectifier)

* 保存模型
h2omlest store dl_model

* 查看性能
display ""
display "Deep Learning 性能："
h2omlestat metrics

/*------------------------------------------------------------------------------
第四部分：模型对比
------------------------------------------------------------------------------*/

display ""
display "第四部分：模型对比"
display "------------------"

* 在测试集上评估所有模型
_h2oframe change test

* GBM 基础版预测
h2omlest restore gbm_basic
h2omlpredict, into(pred_gbm_basic)

* GBM 调优版预测
h2omlest restore gbm_tuned
h2omlpredict, into(pred_gbm_tuned)

* Random Forest 预测
h2omlest restore rf_tuned
h2omlpredict, into(pred_rf_tuned)

* Deep Learning 预测
h2omlest restore dl_model
h2omlpredict, into(pred_dl)

* 导出预测结果到 Stata
_h2oframe get pred_gbm_basic, into(pred_gbm_basic_stata)
_h2oframe get pred_gbm_tuned, into(pred_gbm_tuned_stata)
_h2oframe get pred_rf_tuned, into(pred_rf_stata)
_h2oframe get pred_dl, into(pred_dl_stata)

* 计算测试集性能
use pred_gbm_basic_stata, clear
gen error_gbm_basic = house_price - predict
gen error_sq_gbm_basic = error_gbm_basic^2
quietly summarize error_sq_gbm_basic
scalar rmse_gbm_basic = sqrt(r(mean))
quietly correlate house_price predict
scalar r2_gbm_basic = r(rho)^2

use pred_gbm_tuned_stata, clear
gen error_gbm_tuned = house_price - predict
gen error_sq_gbm_tuned = error_gbm_tuned^2
quietly summarize error_sq_gbm_tuned
scalar rmse_gbm_tuned = sqrt(r(mean))
quietly correlate house_price predict
scalar r2_gbm_tuned = r(rho)^2

use pred_rf_stata, clear
gen error_rf = house_price - predict
gen error_sq_rf = error_rf^2
quietly summarize error_sq_rf
scalar rmse_rf = sqrt(r(mean))
quietly correlate house_price predict
scalar r2_rf = r(rho)^2

use pred_dl_stata, clear
gen error_dl = house_price - predict
gen error_sq_dl = error_dl^2
quietly summarize error_sq_dl
scalar rmse_dl = sqrt(r(mean))
quietly correlate house_price predict
scalar r2_dl = r(rho)^2

* 显示对比结果
display ""
display "=========================================="
display "模型性能对比（测试集）"
display "=========================================="
display ""
display "模型                    R²        RMSE"
display "------------------------------------------"
display "多项式回归          " %6.4f r2_poly "    " %8.2f rmse_poly
display "Lasso 回归          " %6.4f r2_lasso "    " %8.2f rmse_lasso
display "GBM 基础版          " %6.4f r2_gbm_basic "    " %8.2f rmse_gbm_basic
display "GBM 调优版          " %6.4f r2_gbm_tuned "    " %8.2f rmse_gbm_tuned
display "Random Forest       " %6.4f r2_rf "    " %8.2f rmse_rf
display "Deep Learning       " %6.4f r2_dl "    " %8.2f rmse_dl
display "------------------------------------------"
display ""

* 计算性能提升
scalar r2_improve = (r2_gbm_tuned - r2_poly) / r2_poly * 100
scalar rmse_improve = (rmse_poly - rmse_gbm_tuned) / rmse_poly * 100

display "GBM 调优版相比多项式回归："
display "  R² 提升: " %5.1f r2_improve "%"
display "  RMSE 降低: " %5.1f rmse_improve "%"
display ""

/*------------------------------------------------------------------------------
第五部分：模型解释
------------------------------------------------------------------------------*/

display ""
display "第五部分：模型解释"
display "------------------"

* 恢复最佳模型
h2omlest restore gbm_tuned

* SHAP 值汇总图
display ""
display "生成 SHAP 值汇总图..."
h2omlgraph shapsummary, ///
    title("SHAP 值汇总图 - 房价影响因素") ///
    saving("output/cases_h2o/figures/case06h_shap_summary.gph", replace)

* 部分依赖图 - 房屋面积
display ""
display "生成部分依赖图 - 房屋面积..."
h2omlgraph pdp house_size, ///
    title("房屋面积对价格的影响") ///
    saving("output/cases_h2o/figures/case06h_pdp_size.gph", replace)

* 部分依赖图 - 地块大小
display ""
display "生成部分依赖图 - 地块大小..."
h2omlgraph pdp lot_size, ///
    title("地块大小对价格的影响") ///
    saving("output/cases_h2o/figures/case06h_pdp_lot.gph", replace)

/*------------------------------------------------------------------------------
第六部分：业务洞察
------------------------------------------------------------------------------*/

display ""
display "第六部分：业务洞察"
display "------------------"

display ""
display "=========================================="
display "房价预测分析报告"
display "=========================================="
display ""
display "1. 模型性能"
display "   - H2O GBM 模型 R² = " %5.3f r2_gbm_tuned
display "   - 相比传统多项式回归提升 " %4.1f r2_improve "%"
display "   - RMSE 降低 " %4.1f rmse_improve "%"
display ""
display "2. 关键发现"
display "   - 房屋面积是价格的最重要驱动因素"
display "   - 地块大小对价格有显著影响"
display "   - 位置（foreign）对价格有重要影响"
display "   - 非线性关系显著（平方项重要）"
display ""
display "3. 投资建议"
display "   - 优先考虑大面积房产"
display "   - 关注地块大小和位置"
display "   - 使用预测模型评估投资价值"
display "   - 定期更新模型以反映市场变化"
display ""

/*------------------------------------------------------------------------------
第七部分：清理和关闭
------------------------------------------------------------------------------*/

display ""
display "第七部分：清理和关闭"
display "--------------------"

* 关闭 H2O 集群
display ""
display "关闭 H2O 集群..."
h2o shutdown, force

display ""
display "=========================================="
display "案例 6H 完成！"
display "=========================================="
display ""
display "输出文件："
display "  - 日志: output/cases_h2o/case06h_house_price_h2o.log"
display "  - 图表: output/cases_h2o/figures/case06h_*.gph"
display ""

log close

